package com.jw.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jw.system.entity.Dept;
import com.jw.system.entity.Role;
import com.jw.system.entity.User;
import com.jw.system.entity.vo.DepartmentVO;
import com.jw.system.entity.vo.RoleVO;
import com.jw.system.mapper.DeptMapper;
import com.jw.system.mapper.RoleMapper;
import com.jw.system.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 岗位表 服务实现类
 * </p>
 *
 * @author jw
 * @since 2021-04-20
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public Page<RoleVO> getRoleList(Integer current, Integer size, RoleVO roleVO) {
        Page<Role> page = new Page<>(current, size);
        QueryWrapper<Role> queryWrapper = new QueryWrapper();
        if (roleVO.getName() != null && !"".equals(roleVO.getName())) {
            queryWrapper.like("name", roleVO.getName());
        }
        if (roleVO.getDeptId()!=null){
            queryWrapper.like("dept_id",roleVO.getDeptId());
        }
        Page<Role> rolePage = roleMapper.selectPage(page,queryWrapper);
        List<RoleVO> roleVOS  = new ArrayList<>();
        for(Role role:rolePage.getRecords()){
            RoleVO r = new RoleVO();
            BeanUtils.copyProperties(role, r);
            Dept dept = deptMapper.selectById(role.getDeptId());
            if(dept==null)r.setDeptName("无");
            else r.setDeptName(dept.getName());
            roleVOS.add(r);
        }
        Page<RoleVO> roleVOPage = new Page<>();
        roleVOPage.setRecords(roleVOS);
        roleVOPage.setTotal(rolePage.getTotal());

        return roleVOPage;
    }

    @Override
    public List<Role> findAll() {
        return roleMapper.selectList(null);
    }
}
